package com.ilop.sthome.ui.activity.mine;

import android.os.Handler;
import androidx.recyclerview.widget.LinearLayoutManager;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.view.animation.LayoutAnimationController;

import com.ilop.sthome.ui.activity.main.ContainLoginActivity;
import com.example.common.utils.LiveDataBus;
import com.example.common.utils.SpUtil;
import com.ilop.sthome.common.CommonId;
import com.ilop.sthome.data.bean.AssessBean;
import com.ilop.sthome.mvp.contract.mine.AssessContract;
import com.ilop.sthome.mvp.present.mine.AssessPresenter;
import com.ilop.sthome.ui.adapter.main.AssessAdapter;
import com.ilop.sthome.ui.adapter.main.DetectionAdapter;
import com.siterwell.familywellplus.R;
import com.siterwell.familywellplus.databinding.ActivityAssessBinding;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

/**
 * @author skygge
 * @date 2020-02-19.
 * GitHub：javofxu@github.com
 * email：skygge@yeah.net
 * description：安全评测页面
 */
public class AssessActivity extends ContainLoginActivity<AssessPresenter, ActivityAssessBinding> implements AssessContract.IView{

    private int mFraction;
    private Timer mTimer;
    private int mPosition = 0;
    private AssessAdapter mAdapter;
    private DetectionAdapter mAdapter2;
    private Handler mHandler;
    private List<AssessBean.DeviceBen> mDeviceList;
    private List<AssessBean.DeviceBen> mList;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_assess;
    }

    @Override
    protected void initPresent() {
        super.initPresent();
        mPresent = new AssessPresenter(mContext);
    }

    @Override
    protected void initialize() {
        super.initialize();
        immersionStatusBar(false);
        mFraction = SpUtil.getInt(mContext, CommonId.UPDATE_ASSESS_RING, 0);
    }

    @Override
    protected void initView() {
        super.initView();
        mTimer = new Timer();
        mHandler = new Handler();
        mDeviceList = new ArrayList<>();
        mAdapter = new AssessAdapter(mContext);
        mAdapter2 = new DetectionAdapter(mContext);
        LayoutAnimationController animation = AnimationUtils.loadLayoutAnimation(mContext, R.anim.layout_animation);
        mDBind.assessList2.setLayoutManager(new LinearLayoutManager(mContext));
        mDBind.assessList2.setLayoutAnimation(animation);
        mDBind.assessList2.setAdapter(mAdapter2);

        mDBind.assessList.setLayoutManager(new LinearLayoutManager(mContext));
        mDBind.assessList.setAdapter(mAdapter);
    }

    @Override
    protected void initData() {
        super.initData();
        mPresent.startAnalyze();
        if (mFraction>0){
            mDBind.assessEmpty.setVisibility(View.GONE);
            mDBind.assessRing.startCountStep(mFraction);
            showAssessFinish(mFraction);
        }
    }

    @Override
    protected void initDataBus() {
        super.initDataBus();
        LiveDataBus.get().with(CommonId.UPDATE_ASSESS_RING, Integer.class).observe(this, integer -> {
            mDBind.assessRing.startCountStep(mFraction);
            mTimer.schedule(new TimerTask() {
                @Override
                public void run() {
                    if (mPosition == 18){
                        mTimer.cancel();
                        mHandler.post(()-> showAssessFinish(mFraction));
                    }else {
                        mHandler.post(() -> {
                            if (mPosition<=18) {
                                mDeviceList.add(mList.get(mPosition));
                                mAdapter2.setList(mDeviceList);
                                mAdapter2.notifyDataSetChanged();
                                mDBind.assessList2.smoothScrollToPosition(mAdapter2.getItemCount() - 1);
                                mPosition++;
                            }
                        });
                    }
                }
            }, 0, 500);
        });
    }

    @Override
    protected void initListener() {
        super.initListener();
        mDBind.assessBack.setOnClickListener(view -> {
            SpUtil.putInt(mContext, CommonId.UPDATE_ASSESS_RING, mFraction);
            finish();
        });
        mDBind.assessStart.setOnClickListener(v -> {
            mDBind.assessMsg.setVisibility(View.INVISIBLE);
            mDBind.assessEmpty.setVisibility(View.GONE);
            mDBind.assessList2.setVisibility(View.VISIBLE);
            mPresent.getInstallData();
            LiveDataBus.get().with(CommonId.UPDATE_ASSESS_RING).postValue(0);
        });
        mDBind.assessRing.setCallBack(() -> {
            mTimer = new Timer();
            mPresent.getInstallData();
            mDBind.assessMsg.setVisibility(View.INVISIBLE);
            mDBind.assessRound.setVisibility(View.GONE);
            mDBind.assessList2.setVisibility(View.VISIBLE);
            mDeviceList.clear();
            mPosition = 0;
            mDBind.assessRing.setTouchable(false);
            LiveDataBus.get().with(CommonId.UPDATE_ASSESS_RING).postValue(0);
        });
    }

    @Override
    public void showAssessInfo(float fraction, List<AssessBean> beanList) {
        mFraction = Math.round(fraction/128.8f * 100);
        mAdapter.setList(beanList);
    }

    @Override
    public void showInstallInfo(float fraction, List<AssessBean.DeviceBen> list) {
        this.mList = list;
        mFraction = Math.round(fraction/128.8f * 100);
    }

    @Override
    public void showAssessFinish(int fraction) {
        String mMessage;
        if (fraction == 100){
            mMessage = getString(R.string.your_smart_home_score_is_100);
        }else {
            mMessage = String.format(getString(R.string.intellectualization_can_go_up_an), String.valueOf(fraction));
        }
        mPresent.getAssessData();
        mDBind.assessList2.setVisibility(View.GONE);
        mDBind.assessList.setVisibility(View.VISIBLE);
        mDBind.assessRound.setVisibility(View.VISIBLE);
        mDBind.assessMsg.setVisibility(View.VISIBLE);
        mDBind.assessMsg.setText(mMessage);
        mDBind.assessRing.setCurrentStep(fraction);
        mDBind.assessRing.setTouchable(true);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mTimer!=null) mTimer.cancel();
    }
}
